//
//  STMainPageVC.m
//  store
//
//  Created by zhouzhouzhou on 2018/9/26.
//  Copyright © 2018年 zhou. All rights reserved.
//

#import "YLHomeViewController.h"

#import "STMainCollectionView.h"
#import "STServiceManageVC.h"
#import "STHairHouseVC.h"
#import "STHairerInfoVC.h"
#import "LBXScanViewStyle.h"
#import "DIYScanViewController.h"
#import "STCommentMagVC.h"
#import "STOrderStatisticsVC.h"
#import "STHairProsVC.h"
#import "STOrderVC.h"
#import "STTimerManageVC.h"
#import "STMarketingVC.h"
#import "STFirstControl.h"
#import "STComplementedVC.h"
#import "YLIdentityAuthenticationViewController.h"
//#import "YLSoreVerityViewController.h"

@interface YLHomeViewController ()<STMainCollectionViewDelegate>{
    STMainCollectionView * mainCollection;
    NSArray * mainCellArr;
    STOrderVC * orderVC ;
    STDoorExamineModel * examineModel;
    int examineStatus ; //当前审核状态 1-通过 0-待审核 -1-驳回 4-不显示
    int loadExamineNum; //查询审核状态次数
}
@property (weak, nonatomic) IBOutlet UIImageView *advImg;

@property (weak, nonatomic) IBOutlet UILabel *refundLab;
@property (weak, nonatomic) IBOutlet UILabel *reServiceLab;
@property (weak, nonatomic) IBOutlet UILabel *reHandelLab;
@property (weak, nonatomic) IBOutlet UILabel *servicingLab;
@property (weak, nonatomic) IBOutlet UIView *controllerBackView;

@property (weak, nonatomic) IBOutlet UIView *serviceingView;
@property (weak, nonatomic) IBOutlet UIView *reServiceView;
@property (weak, nonatomic) IBOutlet UIView *reHandelView;
@property (weak, nonatomic) IBOutlet UIView *refundView;
@property (strong, nonatomic) UIButton *locationBtn;

//审核界面
@property (weak, nonatomic) IBOutlet UIView *examineView;
@property (weak, nonatomic) IBOutlet UILabel *examineStatusLab;
@property (weak, nonatomic) IBOutlet UILabel *datailLab;
@property (weak, nonatomic) IBOutlet UIButton *goBtn;

@end

@implementation YLHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"";
    
    // 设置初始导航栏透明度
    [self wr_setNavBarBackgroundAlpha:0];
    [self wr_setNavBarShadowImageHidden:YES];
    [self wr_setNavBarBarTintColor:[UIColor whiteColor]];
    [self wr_setNavBarTintColor:[UIColor blackColor]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.locationBtn];
    
    mainCellArr = @[@[@{@"name":@"服务管理",@"img":@"m-service"},@{@"name":@"入驻门店",@"img":@"m-evaluate"},@{@"name":@"时间管理",@"img":@"m-ordered"},@{@"name":@"营销活动",@"img":@"m-hairdresser"},@{@"name":@"我的名片",@"img":@"m-works"},@{@"name":@"扫码核销",@"img":@"m-scan"}],@[@{@"name":@"评价管理",@"img":@"m-hairdresser"},@{@"name":@"订单统计",@"img":@"m-works"},@{@"name":@"我的作品",@"img":@"m-scan"}]];
    // Do any additional setup after loading the view from its nib.
    [self setCollectionView];
    [self setViewAction];
    
    [self getLoaction];
    
    //判断审核状态
    examineStatus = 3;
    [self judgeExamineStatus];
    //打开首页后的 第一次操作记录信息等
    [self firstOpenControll];
}
-(UIButton *)locationBtn{
    if (!_locationBtn) {
        _locationBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 20, 85, 44)];
        _locationBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_locationBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
        _locationBtn.titleLabel.font = FONT(15);
        [_locationBtn setImage:IMAGENAMED(@"info-address") forState:UIControlStateNormal];
        [_locationBtn setTitle:@"" forState:UIControlStateNormal];
    }
    return _locationBtn;
}
-(void)getLoaction{
    [[STFirstControl sharedInstance] setFirstControl];
    [STFirstControl sharedInstance].locationSuccess = ^(AMapLocationReGeocode *selfRegeocode) {
        NSString * area = [NSString stringWithFormat:@" %@",selfRegeocode.district?:@"龙华区"];
        [_locationBtn setTitle:area forState:UIControlStateNormal];
    };

}

-(void)firstOpenControll{
    STFirstControl * control = [STFirstControl sharedInstance];
    [control setFirstControl];
}


//判断审核状态
-(void)judgeExamineStatus{
    
    //先查询是否已经存储了 审核的状态
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    examineStatus =  [[user objectForKey:@"examineStatus"] intValue];
    
    self.goBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.goBtn.layer.borderWidth = 1;
    if(examineStatus == 0){
        self.examineView.hidden = NO;
        self.examineStatusLab.text = @"审核中";
        self.datailLab.text = @"审核时间需要1～3工作日，审核期间不能接受订单，先去完善个人资料吧";
        [self.goBtn setTitle:@"完善门店" forState:UIControlStateNormal];
    }else if(examineStatus == 1){
        self.examineView.hidden = NO;
        self.examineStatusLab.text = @"审核通过";
        self.datailLab.text = @"恭喜！您的审核已经通过，快去加入门店开始服务吧！";
        [self.goBtn setTitle:@"确认" forState:UIControlStateNormal];
    }else if(examineStatus == -1){
        self.examineView.hidden = NO;
        self.examineStatusLab.text = @"审核失败";
        self.datailLab.text = @"抱歉，您的申请没有通过，请重新提交申请";
        [self.goBtn setTitle:@"重新申请" forState:UIControlStateNormal];
    }else if(examineStatus == 4){
        loadExamineNum = 1;
        self.examineView.hidden = YES;
    }
    [self loadExamineData];
}

- (IBAction)actionTurnExamineVC:(id)sender {
    if(examineStatus == 0){
        STComplementedVC * commentVC  =[STComplementedVC new];
        commentVC.hidesBottomBarWhenPushed= YES;
        [self.navigationController pushViewController:commentVC animated:YES];
    }else if(examineStatus == 1){
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:[NSNumber numberWithInt:4] forKey:@"userPassWord"];
        STHairHouseVC * hairHouseVC  =[STHairHouseVC new];
        hairHouseVC.hidesBottomBarWhenPushed= YES;
        [self.navigationController pushViewController:hairHouseVC animated:YES];
    }else if(examineStatus == -1){
        YLIdentityAuthenticationViewController * verifyVC = [[YLIdentityAuthenticationViewController alloc] init];
        verifyVC.hidesBottomBarWhenPushed= YES;
        [self.navigationController pushViewController:verifyVC animated:YES];
    }
}


-(void)loadExamineData{
    if (loadExamineNum) {
        return;
    }
    self.examineView.hidden = YES;
    [STHairService getStylistAuthStatus:^(id result, NSError *error) {
        loadExamineNum = 1;
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        examineModel = [STDoorExamineModel modelWithJSON:result];
        examineStatus = examineModel.status;
        
        //存储审核的状态
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:[NSNumber numberWithInt:examineStatus] forKey:@"examineStatus"];
        [self judgeExamineStatus];
    }];
}

- (void)viewDidLayoutSubviews {
    mainCollection.frame = CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.controllerBackView.bounds));
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)setViewAction{
    UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTurnToMyOrder:)];
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTurnToMyOrder:)];
    UITapGestureRecognizer *tapGesture3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTurnToMyOrder:)];
    UITapGestureRecognizer *tapGesture4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTurnToMyOrder:)];
    
//    self.serviceingView.multipleTouchEnabled = YES;
//    self.reServiceView.userInteractionEnabled = YES;
//    self.reHandelView.multipleTouchEnabled = YES;
    
    [self.serviceingView addGestureRecognizer:tapGesture1];
    [self.reServiceView addGestureRecognizer:tapGesture2];
    [self.reHandelView addGestureRecognizer:tapGesture3];
    [self.refundView addGestureRecognizer:tapGesture4];
    
    
    UITapGestureRecognizer *tapAdvGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTurnToAdv:)];
    
    [self.advImg addGestureRecognizer:tapAdvGesture];
}

-(void)setCollectionView{
    
    mainCollection = [[STMainCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGRectGetHeight(self.controllerBackView.bounds))];
    mainCollection.mainCellArr = mainCellArr;
    mainCollection.delegate = self;
    [mainCollection setMainSpaceCellView];
    [self.controllerBackView addSubview:mainCollection];
}


//广告点击
-(void)actionTurnToAdv:(UIGestureRecognizer *)sender{
     NSLog(@"广告点击");
}

//订单
-(void)actionTurnToMyOrder:(UIGestureRecognizer *)sender{
    if (!orderVC) {
        orderVC = [STOrderVC new];
    }
    int tag = (int)sender.view.tag;
    orderVC.selType = tag - 1001;
    orderVC.hidesBottomBarWhenPushed= YES;
    [self.navigationController pushViewController:orderVC animated:YES];
    
}

//cell点击代理
-(void)mainSpaceView:(STMainCollectionView *)SceneView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==0 ) {
        if (indexPath.row == 0) {
            STServiceManageVC * serviceVC  =[STServiceManageVC new];
            serviceVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:serviceVC animated:YES];
        }else if (indexPath.row == 1) {
            STHairHouseVC * hairHouseVC  =[STHairHouseVC new];
            hairHouseVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:hairHouseVC animated:YES];
        }else if (indexPath.row == 2) {
            STTimerManageVC* timerVC  =[STTimerManageVC new];
            timerVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:timerVC animated:YES];
        }else if (indexPath.row == 3) {
            
            STMarketingVC *marketingVC  =[STMarketingVC new];
            marketingVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:marketingVC animated:YES];
        }else if (indexPath.row == 4) {
            STHairerInfoVC * hairInfoVC  =[STHairerInfoVC new];
            // FIXME:测试参数
            hairInfoVC.stylistId = 1;//[[YLUserInfo sharedInstance].stylistId longValue];
            hairInfoVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:hairInfoVC animated:YES];
        }else if (indexPath.row == 5) {
            DIYScanViewController *vc = [DIYScanViewController new];
            vc.style = [self scanStyle];
            vc.isOpenInterestRect = YES;
            vc.libraryType = SLT_Native;
            vc.scanCodeType = SCT_QRCode;
            vc.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
       
    }else if(indexPath.section == 1){
        if (indexPath.row == 0) {
            STCommentMagVC * commentVC  =[STCommentMagVC new];
            commentVC.stylistId = [[YLUserInfo sharedInstance].stylistId longValue];
            commentVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:commentVC animated:YES];
        }else if (indexPath.row == 1) {
            STOrderStatisticsVC * orderStatisticsVC  =[STOrderStatisticsVC new];
            orderStatisticsVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:orderStatisticsVC animated:YES];
        }else if (indexPath.row == 2) {
            STHairProsVC * proVC  =[STHairProsVC new];
            proVC.stylistId = 1;
            proVC.hidesBottomBarWhenPushed= YES;
            [self.navigationController pushViewController:proVC animated:YES];
        }
    }
    NSLog(@"首页cell点击");
 
}


-(LBXScanViewStyle *)scanStyle{
    //设置扫码区域参数
    LBXScanViewStyle *style = [[LBXScanViewStyle alloc]init];
    
    style.centerUpOffset = 44;
    style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle_Inner;
    style.photoframeLineW = 2;
    style.photoframeAngleW = 18;
    style.photoframeAngleH = 18;
    style.isNeedShowRetangle = YES;
    style.anmiationStyle = LBXScanViewAnimationStyle_LineMove;
    style.colorAngle = [UIColor colorWithRed:0./255 green:200./255. blue:20./255. alpha:1.0];
    
    //qq里面的线条图片
    UIImage *imgLine = [UIImage imageNamed:@"CodeScan.bundle/qrcode_Scan_weixin_Line"];
    style.animationImage = imgLine;
    
    style.notRecoginitonArea = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    
    
    return style;
}


@end
